package ldkxingzhe.l365calendar.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;

/**
 * 主页上的更多界面
 * Created by ke on 16-1-16.
 */
public class MainMore extends Fragment{
    @SuppressWarnings("unused")
    private final static String TAG = "MainMore";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_main_more, container, false);
        initTitle(resultView.findViewById(R.id.title));
        initLogoLayout(resultView.findViewById(R.id.logo_layout));
        initBirthLayout(resultView.findViewById(R.id.birth_slq_layout));
        initSettingLayout(resultView.findViewById(R.id.setup_layout));
        initSyncLayout(resultView.findViewById(R.id.sync_layout));
        return resultView;
    }

    private void initSyncLayout(View syncLayout) {
        topLayoutSetting(syncLayout, R.drawable.more_sync_icon, R.string.sync, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "sync was clicked");
            }
        });
        syncLayout.findViewById(R.id.bottom_layout).setVisibility(View.GONE);
        syncLayout.findViewById(R.id.more_item_middle_line).setVisibility(View.GONE);
        TextView topRightText = (TextView) syncLayout.findViewById(R.id.more_item_right_first_text);
        topRightText.setVisibility(View.VISIBLE);
        topRightText.setText(R.string.sync_need_login);
    }

    private void initSettingLayout(View settingLayout) {
        topLayoutSetting(settingLayout, R.drawable.more_setup_icon, R.string.setting, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "setting was clicked");
            }
        });
        bottomLayoutSetting(settingLayout, R.drawable.my_more_calendar_icon, R.string.about, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "about 365 calendar was clicked");
            }
        });
    }

    private void topLayoutSetting(View parentLayout, int imageResource, int stringId, View.OnClickListener listener){
        View topLayout = parentLayout.findViewById(R.id.top_layout);
        ((ImageView)topLayout.findViewById(R.id.more_item_top_icon)).setImageResource(imageResource);
        ((TextView)topLayout.findViewById(R.id.more_item_top_text)).setText(stringId);
        topLayout.setOnClickListener(listener);
    }
    private void bottomLayoutSetting(View parentLayout, int imageResource, int stringId, View.OnClickListener listener){
        View bottomLayout = parentLayout.findViewById(R.id.bottom_layout);
        ((ImageView)bottomLayout.findViewById(R.id.more_item_bottom_icon)).setImageResource(imageResource);
        ((TextView)bottomLayout.findViewById(R.id.more_item_bottom_text)).setText(stringId);
        bottomLayout.setOnClickListener(listener);
    }

    private void initBirthLayout(View birthLayout) {
        topLayoutSetting(birthLayout, R.drawable.more_birthday_icon, R.string.birthday_note, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "on birthday note book click");
            }
        });
        bottomLayoutSetting(birthLayout, R.drawable.more_slq_icon, R.string.slq, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "on slq click");
            }
        });
    }

    private void initLogoLayout(View logoLayout) {
        logoLayout.findViewById(R.id.bottom_layout).setVisibility(View.GONE);
        logoLayout.findViewById(R.id.more_item_middle_line).setVisibility(View.GONE);
        topLayoutSetting(logoLayout, R.drawable.account_logo_default, R.string.loginImmediately, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "onLogoInClick");
            }
        });
    }

    private void initTitle(View titleLayout) {
        titleLayout.findViewById(R.id.btn_title_left).setVisibility(View.GONE);
        titleLayout.findViewById(R.id.btn_title_right).setVisibility(View.GONE);
        Button titleButton = (Button) titleLayout.findViewById(R.id.btn_title_middle);
        titleButton.setText(getString(R.string.more));
        titleButton.setFocusable(false);
    }
}
